package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.dao.NodeDao;
import com.lkd.entity.BusinessTypeEntity;
import com.lkd.entity.NodeEntity;
import com.lkd.entity.RegionEntity;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.exception.LogicException;
import com.lkd.http.controller.vo.NodeReq;
import com.lkd.service.BusinessTypeService;
import com.lkd.service.NodeService;
import com.lkd.service.RegionService;
import com.lkd.service.VendingMachineService;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.LoginException;
import java.util.List;


@Service
public class NodeServiceImpl extends ServiceImpl<NodeDao, NodeEntity> implements NodeService {

    @Autowired
    private RegionService regionService;

    @Autowired
    private BusinessTypeService businessTypeService;

    @Autowired
    private VendingMachineService vendingMachineService;

    /*
    * 点位分页查询
    * */
    @Override
    public Pager<NodeEntity> search(Integer pageIndex, Integer pageSize, String name, Long regionId) {
      //1. 点位分页+条件
        // 1-1 创建分页
        Page<NodeEntity> page = new Page<>(pageIndex, pageSize);
        //1-2 构建条件
        LambdaQueryWrapper<NodeEntity> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),NodeEntity::getName,name);
        qw.eq(ObjectUtil.isNotEmpty(regionId),NodeEntity::getRegionId,regionId);
        //1-3 查询
        this.page(page);
        //2. 遍历点位 填充 售货机数量、区域信息、商圈信息
        for (NodeEntity nodeEntity : page.getRecords()) {
            // 2-1 售货机数量 select count(*) from tb_vending_machine where node_id=?
            LambdaQueryWrapper<VendingMachineEntity> vmqw = new LambdaQueryWrapper<>();
            vmqw.eq(VendingMachineEntity::getNodeId,nodeEntity.getId());
            int vmCount = vendingMachineService.count(vmqw);
            nodeEntity.setVmCount(vmCount);
            //2-2 区域信息 select * from tb_region where id =?
            RegionEntity regionEntity = regionService.getById(nodeEntity.getRegionId());
            nodeEntity.setRegion(regionEntity);
            //2-3 商圈信息
            BusinessTypeEntity businessTypeEntity = businessTypeService.getById(nodeEntity.getBusinessId());
            nodeEntity.setBusinessType(businessTypeEntity);
        }
        //3.返回分页对象
        return Pager.build(page);
    }

    /*
    * 点位详情
    * */
    @Override
    public List<VendingMachineEntity> vmList(Long id) {
       //根据点位id作为条件查询售货机列表
        // 1-1 构建条件
        LambdaQueryWrapper<VendingMachineEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(VendingMachineEntity::getNodeId,id);
        //1-2 查询列表
        return vendingMachineService.list(qw);
    }

    /*
    * 点位新增
    * */
    @Override
    public Boolean saveNode(NodeReq nodeReq) {
        //1. 业务校验(商圈是否存在、区域是否存在、节点名是否重复)
         // 1-1 商圈是否存在
        BusinessTypeEntity businessTypeEntity = businessTypeService.getById(nodeReq.getBusinessId());
        if(ObjectUtil.isEmpty(businessTypeEntity)){
            throw new LogicException("商圈不存在!");
        }
        //1-2 区域是否存在
        RegionEntity regionEntity = regionService.getById(nodeReq.getRegionId());
        if(ObjectUtil.isEmpty(regionEntity)){
            throw new LogicException("区域不存在!");
        }
        //1-3 节点名是否重复
        LambdaQueryWrapper<NodeEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(NodeEntity::getName,nodeReq.getName());
        int nodeCount = this.count(qw);
        if(nodeCount>0){
            throw new LogicException("点位重名");
        }
        //2.保存数据库(dto->po)
        NodeEntity nodeEntity = BeanUtil.copyProperties(nodeReq, NodeEntity.class);
        //3. 返回boolean结果
        return this.save(nodeEntity);
    }

    // 点位修改
    @Override
    public Boolean updateNode(Long id, NodeReq nodeReq) {
       //1. 业务功能(商圈是否存在、区域是否存在、节点名是否重复)
         //1-1 商圈是否存在
        BusinessTypeEntity businessTypeEntity = businessTypeService.getById(nodeReq.getBusinessId());
        if(ObjectUtil.isEmpty(businessTypeEntity)){
            throw new LogicException("商圈不存在!");
        }
        //1-2 区域是否存在
        RegionEntity regionEntity = regionService.getById(nodeReq.getRegionId());
        if(ObjectUtil.isEmpty(regionEntity)){
            throw new LogicException("区域不存在");
        }
        //1-3 节点名称是否重复 select count(*) from tb_node where name=? and id != 你自己节点的id
        LambdaQueryWrapper<NodeEntity> qw = new LambdaQueryWrapper<>();
        qw.eq(NodeEntity::getName,nodeReq.getName());
        qw.eq(NodeEntity::getId,id);
        int nodeCount = this.count(qw);
        if(nodeCount > 1){
            throw new LogicException("点位重名");
        }
        NodeEntity nodeEntity = BeanUtil.copyProperties(nodeReq, NodeEntity.class);
        //补充这个id字段
        nodeEntity.setId(id);
        //3.返回boolean结果
        return this.updateById(nodeEntity);
    }

    /*
    * 点位删除
    * */
    @Override
    public Boolean deleteNode(Long id) {
     //1. 校验该点位下是否有售货机 select count(*) from tb_vending_machine where node_id=?
        LambdaQueryWrapper<VendingMachineEntity> vmqw = new LambdaQueryWrapper<>();
        vmqw.eq(VendingMachineEntity::getNodeId,id);
        int vmCount = vendingMachineService.count(vmqw);
        if(vmCount>0){
            throw new LogicException("该点位下有售货机，无法删除!");
        }
        //2.删除并返回结果
        return this.removeById(id);
    }


}
